﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace Empires.Infrastructure.Entities.Repository
{
    [Table("T_SiteReference")]
    public class T_SiteReference : IEntity<T_SiteReference>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 对应公司Id
        /// </summary>
        public int CopId { get; set; }
        /// <summary>
        /// 预设二级域名URL
        /// </summary>
        [Column(TypeName="varchar")]
        [StringLength(128)]
        public string SetURL { get; set; }
        /// <summary>
        /// 独立购买域名
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string SpecialURL { get; set; }

        public override T_SiteReference Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_SiteReference Find(System.Linq.Expressions.Expression<Func<T_SiteReference, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.SiteReference.FirstOrDefault(where);
                if (item != null)
                {
                    this.CopId = item.CopId;
                    this.Id = item.Id;
                    this.SetURL = item.SetURL;
                    this.SpecialURL = item.SpecialURL;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_SiteReference, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.SiteReference.Any(where);
            }
        }
    }
}
